var tableId = "";
$(function(){
	var args = window.location.search;
	tableId = args.split("=")[1];
	var tableName;
	$.ajax({
    	type : 'post',
		dataType: 'json',
		contentType: "application/json",
		async: false, 
        url: '../../sti.do?action=5&tableId=' + tableId,  
        success: function(data) { 
        	tableName = data.tableName;
			$("#txtTableName").val(tableName);
			$("#txtCreateUser").val(data.createUser);
			$("#txtCreateTime").val(data.createTime);
			$("#txtModifyTime").val(data.modifyTime);
			$("#spanTableDesc").val(data.tableDesc);
			$("#dg").attr("title", "fdasfdas");
        }  
    }); 
	
	var fieldtypes;
	$.ajax({
    	type : 'get',
		dataType: 'json',
		contentType: "application/json",
		async: false, 
        url: '../../sft.do?action=0',  
        success: function(data) { 
        	fieldtypes = "[" + JSON.stringify(data) + "]";
        }  
    }); 
	       
	fieldtypes = [
	              {typeId:'varchar',typeName:'varchar'}
	              ];
	$('#dg').datagrid({
        title:tableName + '字段设计',
        iconCls:'icon-save',
        width:1200,
        sortName:'fieldId',
        sortOrder:'asc',
        url: '../../stf.do?action=0&tableId=' + tableId,
        columns:[[   
     		        {field:'fieldId',title:'项目序号',width:55,align:'center',sortable: true,  
     		        	sorter: function(a,b){
     		        		iSorter(a,b);
						}},
     		        {field:'fieldChineseName',title:'字段中文名',width:100,align:'center',editor:{type:'text'}},
     		        {field:'fieldEnglishName',title:'字段英文名',width:100,align:'center',editor:{type:'text'}},
     		        {field:'fieldType',title:'类型',width:150,align:'center',editor:{type:'combobox',options:{
						valueField:'typeId',
						textField:'typeName',
						data: fieldtypes
					}}},
     		        {field:'fieldLength',title:'位数',width:40,align:'center',editor:{type:'numberbox'}},
     		        {field:'isNull',title:'非空',width:40,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
     		        {field:'isPrimaryKey',title:'主键',width:40,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
     		        {field:'fieldRelation',title:'外部关系',width:40,align:'center',editor:{type:'text'}},
     		        {field:'isUniqueIndex',title:'唯一索引',width:60,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
     		        {field:'isIndex',title:'索引',width:50,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
     		        {field:'isAutoAdd',title:'自增',width:50,align:'center',editor:{type:'checkbox',options:{on:'1',off:'0'}}},
     		        {field:'fieldInitValue',title:'默认',width:100,align:'center',editor:{type:'text'}},
     		        {field:'fieldStyle',title:'字段格式',width:100,align:'center',editor:{type:'text'}},
     		        {field:'fieldValueYueShu',title:'值约束',width:100,align:'center',editor:{type:'text'}},
     		        {field:'fieldDesc',title:'项目意义',width:100,align:'center',editor:{type:'textarea'}}
     		    ]]
    });
	
});

function btnReturn(){
	window.location.href = "tableInfo.jsp";
}

var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){
		return true;
	}
	if ($('#dg').datagrid('validateRow', editIndex)){
		var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'fieldType'});
		var fieldType = $(ed.target).combobox('getText');
		$('#dg').datagrid('getRows')[editIndex]['typeName'] = fieldType;
		$('#dg').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onClickRow(index){
	if (editIndex != index){
		if (endEditing()){
			$('#dg').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			$('#dg').datagrid('selectRow', editIndex);
		}
	}
}
function append(){
	if (endEditing()){
		$('#dg').datagrid('appendRow',{isNull: '1'});
		editIndex = $('#dg').datagrid('getRows').length-1;
		$('#dg').datagrid('selectRow', editIndex)
				.datagrid('beginEdit', editIndex);
	}
}
function removeit(){
	if (editIndex == undefined){
		return;
	}
	$('#dg').datagrid('cancelEdit', editIndex)
			.datagrid('deleteRow', editIndex);
	editIndex = undefined;
}

function btnGenerateField(){
	var size = $("#txtNum").val();
	$.ajax({
		type : 'post',
		dataType: 'json',
		contentType: "application/json",
		async: false, 
	    url: '../../stf.do?action=4&tableId='+tableId +"&size="+size,  
	    success: function(data) { 
	        window.location.reload();
	    	
	    }  
	}); 
}
function btnReady(){	
	$.layer({
		shade : [0.5 , '#000' , true],
		type : 1,
		area : ['200','100'],
		title : false,
		border : [0],
		page : {dom : '#hidNum'},
		close : function(index){
			layer.close(index);
			$('#hidNum').hide();
		}
	});
}
function accept(){
	if (endEditing()){
		var changeRows = $('#dg').datagrid('getChanges').length;
		if(changeRows != 0){
			$('#dg').datagrid('acceptChanges');
			var rowData = $('#dg').datagrid('getSelected');  
            var json = JSON.stringify(rowData);
            $.ajax({
            	type : 'post',
        		dataType: 'json',
        		contentType: "application/json",
        		data: json,
        		async: false, 
                url: '../../stf.do?action=1&tableId='+tableId,  
                success: function(data) { 
                    window.location.reload();
                	fieldtypes = "[" + JSON.stringify(data) + "]";
                }  
            }); 
		}else{
			layer.alert('没有可保存的数据', 8);
		}
	}
}

function reject(){
	$('#dg').datagrid('rejectChanges');
	editIndex = undefined;
}
function getChanges(){
	var rows = $('#dg').datagrid('getChanges');
	alert(rows.length+' rows are changed!');
}

function createTableSql(){
	$.layer({
	    type: 2,
	    title: false,
		iframe: {src: 'tablesql.jsp?tableId='+tableId},
	    area : ['800px' , '600px'],
	    success : function(){	
	    }
	}); 
}

function createTableObject(){
	$.layer({
	    shade : [0], //不显示遮罩
	    area : ['auto','auto'],
	    dialog : {
	        msg:'你需要导出.java文件吗？',
	        btns : 2, 
	        type : 4,
	        btn : ['不需要','需要'],
	        yes : function(){
	        	$.layer({
	        	    type: 2,
	        	    title: false,
	        		iframe: {src: 'tableobject.jsp?tableId='+tableId},
	        	    area : ['800px' , '600px'],
	        	    success : function(){	
	        	    }
	        	}); 
	        },
	        no : function(){
	        	
	        }
	    }
	});
	
}

function btnSql(){
	window.location.href = "sql.jsp?tableId="+tableId;
}

function iSorter(a,b){
	if(a > b){
		return 1;
	}else{
		return -1;
	}
}

function createClass(){
	window.location.href = "class.jsp?tableId="+tableId;
}

window.onload = function() {
	$('.selectpicker').selectpicker();
};